#include <string.h>
#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;

class Singleton{
public:
    static Singleton * getInstance(){
        pthread_once(&_once,init);
        return _pInstance;
    }
    void print(){
        cout << this << endl;
    }

private:
    static void destory(){
        if(_pInstance){
            delete _pInstance;
            _pInstance = nullptr;
        }
    }
private:
    Singleton(){}
    ~Singleton(){}
    Singleton(const Singleton & rhs) = delete;
    Singleton & operator=(const Singleton & rhs) = delete;
    static Singleton * _pInstance;
    static pthread_once_t _once;
    static void init(){
        _pInstance = new Singleton();
        atexit(destory);
    }
};
Singleton * Singleton::_pInstance = nullptr;
pthread_once_t Singleton::_once = PTHREAD_ONCE_INIT;

void test(){
    Singleton::getInstance()->print();
    Singleton::getInstance()->print();
}

int main()
{
    test();
    return 0;
}

